package com.lu.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lu.project.common.PageResponse;
import com.lu.project.mapper.DeviceDataMapper;
import com.lu.project.model.dto.device.DeviceDataDto;
import com.lu.project.model.entity.DeviceData;
import com.lu.project.model.entity.User;
import com.lu.project.model.vo.DeviceDataVo;
import com.lu.project.service.DeviceDataService;
import com.lu.project.utils.UserThreadLocal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements DeviceDataService {

    @Resource
    private DeviceDataMapper deviceDataMapper;

    @Override
    public void createDeviceData(DeviceDataDto deviceDataDto) {
        deviceDataMapper.insert(BeanUtil.toBean(deviceDataDto, DeviceData.class));
    }

    @Override
    public DeviceDataVo readDeviceData(Long id) {
//        DeviceData deviceData = deviceDataMapper.selectByPrimaryKey(id);
//        return BeanUtil.toBean(deviceData,DeviceDataVo.class);
        // alarmTime 的原始格式：ISO 8601 格式（"2025-02-17T22:32:41"）
        return deviceDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateDeviceData(Long id, DeviceDataDto deviceDataDto) {
        DeviceData deviceData = BeanUtil.toBean(deviceDataDto, DeviceData.class);
        //从当前线程中获取用户数据
//        String subject = UserThreadLocal.getSubject();
//        User user = JSON.parseObject(subject, User.class);
        //deviceData.setProcessor(user.getRealName());
        deviceData.setStatus("3");
        deviceData.setId(id);
        deviceDataMapper.updateByPrimaryKeySelective(deviceData);
    }

    @Override
    public void deleteDeviceData(Long id) {
        deviceDataMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(Integer pageNum, Integer pageSize, Integer status, String deviceName, String accessLocation, Integer locationType, String functionId, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceDataVo> page = deviceDataMapper.page(status, deviceName, accessLocation, locationType, functionId, startTime, endTime);
        for (DeviceDataVo deviceDataVo : page) {
            if ("BloodPressure".equals(deviceDataVo.getFunctionName())) {
                if (Integer.parseInt(deviceDataVo.getDataValue()) >= 90 && Integer.parseInt(deviceDataVo.getDataValue()) <= 115) {
                    deviceDataVo.setDataStatus("0");
                } else if (Integer.parseInt(deviceDataVo.getDataValue()) < 90) {
                    deviceDataVo.setDataStatus("2");
                } else if (Integer.parseInt(deviceDataVo.getDataValue()) > 115) {
                    deviceDataVo.setDataStatus("1");
                }
            } else if ("HeartRate".equals(deviceDataVo.getFunctionName())) {
                if (Integer.parseInt(deviceDataVo.getDataValue()) >= 65 && Integer.parseInt(deviceDataVo.getDataValue()) <= 90) {
                    deviceDataVo.setDataStatus("0");
                } else if (Integer.parseInt(deviceDataVo.getDataValue()) < 65) {
                    deviceDataVo.setDataStatus("2");
                } else if (Integer.parseInt(deviceDataVo.getDataValue()) > 90) {
                    deviceDataVo.setDataStatus("1");
                }
            } else if ("BodyTemperature".equals(deviceDataVo.getFunctionName())) {
                if (Integer.parseInt(deviceDataVo.getDataValue()) >= 35 && Integer.parseInt(deviceDataVo.getDataValue()) <= 37) {
                    deviceDataVo.setDataStatus("0");
                } else if (Integer.parseInt(deviceDataVo.getDataValue()) < 35) {
                    deviceDataVo.setDataStatus("2");
                } else if (Integer.parseInt(deviceDataVo.getDataValue()) > 37) {
                    deviceDataVo.setDataStatus("1");
                }
            }

        }
        return PageResponse.of(page, DeviceDataVo.class);
    }

    @Override
    public PageResponse<DeviceDataVo> getDeviceWeekDataPage(Integer pageNum, Integer pageSize, Integer status, String deviceName, String accessLocation, Integer locationType, String functionId, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceDataVo> page = deviceDataMapper.pageWeek(status, deviceName, accessLocation, locationType, functionId, startTime, endTime);
        return PageResponse.of(page, DeviceDataVo.class);
    }
}
